package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:  到登录页面
 *Date:Crated in 9:35 2021/9/30
 */

import com.liu.community.cache.HotTagsCache;
import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.pojor.Question;
import com.liu.community.pojor.TagResponseDto;
import com.liu.community.pojor.User;
import com.liu.community.server.QuestionService;
import com.liu.community.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class IndexController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private HotTagsCache hotTagsCache;

    //到首页
    @RequestMapping({"/","/index.html","/index"})
    public  String  goIndexPage(HttpServletRequest request, Model model,
                                @RequestParam(name = "currentPage",defaultValue = "1")Integer currentPage,
                                @RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize,
                                @RequestParam(name = "search",required = false)String search,
                                @RequestParam(name = "tag",required = false)String tag                                                                 ){
        List<String> hots = hotTagsCache.getHots();
        List<TagResponseDto> hotTagsCacheResponseDtos = hotTagsCache.getResponseDtos();
        PageDto<QuestionDto> pageDto = questionService.queryAllQuestions(tag,search,currentPage, pageSize);
        model.addAttribute("questionList",pageDto);
        model.addAttribute("search",search);
        model.addAttribute("hots",hots);
        model.addAttribute("tag",tag);
        model.addAttribute("hotTagsCacheResponseDtos",hotTagsCacheResponseDtos);
        return  "index";
    }



}
